/*
 * @(#) DataState
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br> @author caijinhuang
 * <br> 2017-12-18 16:15:55
 */

package com.sunsharing.sharemeeting.constant;


import com.sunsharing.sharemeeting.entity.AttenderState;
import com.sunsharing.sharemeeting.entity.DecisionState;
import com.sunsharing.sharemeeting.entity.Department;
import com.sunsharing.sharemeeting.entity.Role;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.sunsharing.sharemeeting.entity.User;
import com.sunsharing.sharemeeting.service.api.StaticService;

/**
 * 系统预加载数据库部门数据到内存中，
 */
@Component
public class DataState {
    @Resource
    private StaticService staticService;
    /**
     * 所有用户基本信息
     */
    private static List<User> ulist;
    /**
     * 部门信息
     */
    private static List<Department> departmentList;
    /**
     * 角色信息
     */
    private static List<Role> roleList;
    /**
     * 会议出勤状态
     */
    private static List<AttenderState> attendeStateList;
    /**
     * 会议决议优先级
     */
    private static List<DecisionState> decisionStateList;

    private static Map<String,Object> map ;
    /**
     * 数据库数据与查询
     */

    /**
     * 单利模式获取取所有基本信息
     * @return
     */
    public synchronized Map<String,Object> getAll() {
        if (map == null) {
            map = new HashMap<String,Object>();
            ulist = staticService.selectAllUser();
            departmentList = staticService.selectAllDepartment();
            roleList = staticService.selectAllRole();
            attendeStateList = staticService.selectAllAttenderState();
            decisionStateList = staticService.selectAllDecisionStates();
            decisionStateList.remove(4);
            map.put("users",ulist);
            map.put("departments",departmentList);
            map.put("decisionStates",decisionStateList);
            map.put("roles",roleList);
            map.put("attendeState",attendeStateList);
        } else {
            ulist = staticService.selectAllUser();
            map.put("users",ulist);
        }
        return map;
    }


    public List<User> getUlist() {
        if (ulist == null) {
            getAll();
        }
        return ulist;
    }

    public List<Department> getDepartmentList() {
        if (departmentList == null) {
            getAll();
        }
        return departmentList;
    }

    public List<Role> getRoleList() {
        if (roleList == null) {
            getAll();
        }
        return roleList;
    }

    public List<AttenderState> getAttendeStateList() {
        if (attendeStateList == null) {
            getAll();
        }
        return attendeStateList;
    }

    public List<DecisionState> getDecisionStateList() {
        if (departmentList == null) {
            getAll();
        }
        return decisionStateList;
    }
}
